package com.pospi.xstore.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pospi.xstore.R;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.DoubleSave;

import java.util.IllegalFormatCodePointException;
import java.util.List;

/**
 * Created by Qiyan on 2016/6/13.
 */
public class ScanReceiptAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsDtos;

    public ScanReceiptAdapter(Context context, List<GoodsDto> goodsDtos) {
        this.context = context;
        this.goodsDtos = goodsDtos;
    }

    @Override
    public int getCount() {
        return goodsDtos.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsDtos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.scan_receipt_item, null);
            holder.code = (TextView) convertView.findViewById(R.id.tv_pay_code);
            holder.name = (TextView) convertView.findViewById(R.id.tv_good_name);
            holder.price = (TextView) convertView.findViewById(R.id.tv_price);
            holder.num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.discount = (TextView) convertView.findViewById(R.id.tv_discount);
            holder.total = (TextView) convertView.findViewById(R.id.tv_sum);
            holder.tv_limitnum = convertView.findViewById(R.id.tv_limitnum);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GoodsDto dto = goodsDtos.get(position);
        if (dto.getProType().equals(Constant.PRO_ZP)) {
            holder.code.setText("[赠品]");
            holder.tv_limitnum.setText("");
            holder.name.setText(dto.getName());
            holder.price.setText(String.valueOf(dto.getPrice()));
            holder.discount.setText(String.valueOf(DoubleSave.doubleSaveTwo(dto.getDiscount())));
            holder.num.setText(String.valueOf(dto.getNum()));
            holder.total.setText(String.valueOf(DoubleSave.doubleSaveTwo(dto.getNum() * dto.getPrice() - dto.getDiscount())));
        }else {
            if (dto.getSingnum() < 9999 && !dto.getProType().equals("")) {//限量提醒
                holder.tv_limitnum.setText("每单限量" + dto.getSingnum() + "份优惠");
            } else {
                if (dto.getProType().equals(Constant.PRO_N_REDUCE)) {
                    holder.tv_limitnum.setText("第" + dto.getMuchnum() + "件" + dto.getProPrice() + "元");
                } else {
                    holder.tv_limitnum.setText("");
                }

            }
            holder.code.setText(dto.getCode());
            holder.name.setText(dto.getName());
            holder.price.setText(String.valueOf(dto.getPrice()));
            holder.discount.setText(String.valueOf(DoubleSave.doubleSaveTwo(dto.getDiscount())));
            holder.num.setText(String.valueOf(dto.getNum()));
            holder.total.setText(String.valueOf(DoubleSave.doubleSaveTwo(dto.getNum() * dto.getPrice() - dto.getDiscount())));
        }


        return convertView;
    }

    class ViewHolder {
        TextView code;
        TextView name;
        TextView price;
        TextView num;
        TextView discount;
        TextView total;
        TextView tv_limitnum;
    }
}
